﻿namespace EFS.RC.CC.Presences.Operators
{
	class Anonymous : EFS.RC.CC.Presences.Operators.Presence
	{
		// AD user
		private EFS.AD.User adUser = null;

		/// <summary>
		/// Cosntructor
		/// </summary>
		/// <param name="sipUri"></param>
		public Anonymous(System.String sipUri)
		{
			// check
			EFS.Common.Verify.String(sipUri, "operatorDeployment");

			// log annon call
			EFS.EventLog.Log.LogInfo(System.String.Format("Annonimous Operator : {0}", sipUri), this);

			// get ad domain
			EFS.AD.Domain adDomain = EFS.RC.CC.Platform.Instance.EndPointRegistrar.OficeServicePresencesRegistrar.OperatorOfficeServiceEndPointPresence.PresenceEndPointApplication.Domain;

			// sip or tel
			if (sipUri.StartsWith("sip"))
			{
				// SIP
				adUser = adDomain.GetBySIP(sipUri);
			}
			else if (sipUri.StartsWith("tel"))
			{
				// TEL
				adUser = adDomain.GetByTEL(sipUri);
			}
			else if (sipUri.StartsWith("ext"))
			{
				// EXT
				adUser = adDomain.GetByEXT(sipUri);
			}

			// check if user found by sup Uri
			if (null == adUser)
			{
				// get sip domain
				System.String sipDomain = EFS.RC.CC.Platform.Instance.EndPointRegistrar.OficeServicePresencesRegistrar.OperatorOfficeServiceEndPointPresence.PresenceEndPointApplication.CollaborationApplicationEndpoint.DefaultDomain;

				// get user uri
				System.String userUri = EFS.RC.Common.Utilities.UserUri(sipUri, sipDomain);

				// sip or tel
				if (userUri.StartsWith("sip"))
				{
					// SIP
					adUser = adDomain.GetBySIP(userUri);
				}
				else if (userUri.StartsWith("tel"))
				{
					// TEL
					adUser = adDomain.GetByTEL(userUri);
				}
				else if (userUri.StartsWith("ext"))
				{
					// EXT
					adUser = adDomain.GetByEXT(userUri);
				}
			}

			// check
			if (null == adUser)
			{
				//throw new System.InvalidOperationException(System.String.Format("Could not find AD user for Annonimous Operator : {0}", sipUri));
				adUser = new AD.User(System.Guid.Empty, "Unknown", "Unknown", sipUri, null, "Unknown", "Unknown", "Unknown");
			}
		}

		/// <summary>
		/// AD User: Get
		/// </summary>
		public override EFS.AD.User User
		{
			get { return adUser; }
		}
	}
}
